<template>
  <div class="check-task">
    <div>
      <!-- 报告模板 -->
      <eagle-radio-group
        v-model="data.ReportCode"
        required 
        name="radio" 
        label="报告模板"
        :dataSource="data.ReportDefines"
      ></eagle-radio-group>
      <eagle-radio-group
        v-if="codesList.length > 1 && type == 'Report'"
        required
        name="radio"
        label="生成设置"
        v-model="data.GenerateOption"
      >
        <eagle-radio name="2" style="margin: 0 10px 10px 0"
          >合并生成一个报告</eagle-radio
        >
        <eagle-radio name="1" style="margin: 0 10px 10px 0">
          每个任务独立生成报告
        </eagle-radio>
      </eagle-radio-group>
     <eagle-input
      v-if="data.GenerateOption == 2"
      label="报告名称"
      required
      v-model="data.Name"
      rows="3"
      autosize
      type="textarea"
      placeholder="请输入报告名称"
      isInputText
     ></eagle-input>
     <eagle-radio-group
      v-model="data.OrderType"
      name="radio"
      label="隐患排序类型"
      :dataSource="data.OrderTypeList"
      direction="horizontal"
     ></eagle-radio-group>
      <!-- 日期 Date-->
      <eagle-date
        label="开始日期"
        :date.sync="data.StartDate"
        required
        placeholder="点击选择日期"
      ></eagle-date>
      <eagle-date
        label="结束日期"
        :date.sync="data.EndDate"
        required
        placeholder="点击选择日期"
      ></eagle-date>
      <eagle-input
        :multiRow="true"
        title="总结和建议"
        type="textarea"
        v-model="data.CheckRequirement"
        rows="3"
        autosize
        isInputText
        placeholder="请输入总结和建议"
      ></eagle-input>

      <div class="bottom-btns">
        <eagle-button class="submit-btn" type="primary" @click="save()">
          生成
        </eagle-button>
      </div>
    </div>
  </div>
</template>

<script>
import { formatDate, getDate } from "@/utils/getDate";

export default {
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      title: "",
      codes: this.$route.query.codes,
      type: this.$route.query.type,
      filter: {
        id: 0,
        type: "Task",
        lgType: "normal",
        reportType: this.$route.query.reportType||1,
      },
      data: {},
      uploader: [{ url: "https://img.yzcdn.cn/vant/leaf.jpg" }],
      showCalendar: false,
      idx: "",
      show: false,
      showChooseTask: false,
      activeIcon: require("@/assets/Images/AppIcon/item_check_transparent.png"),
      codesList: [],
      reportCodeList:[],
    };
  },
  created() {
    if (this.$route.query.codes) {
      this.getGenerateEntity();
      let codesList = this.codes.split(",");
      this.codesList = codesList;
    }
  },
  activated() {},
  methods: {
    getGenerateEntity() {
      this.filter.codes = this.codes;
      let _this = this;
      _this.$axios
      .get("/OtherDangerReportJG/GetGenerateEntity",{params:_this.filter})
      .then((res)=>{
        res.data.OrderTypeList.unshift({
          ID: 0,
          Name: "不限",
        });
        res.data.OrderType = 0;
        res.data.ReportCode = res.data.ReportDefines[0].ID;
        res.data.StartDate = getDate(res.data.StartDate);
        res.data.EndDate = getDate(res.data.EndDate);
        _this.data = res.data;
      })
    },
    // 选择检查类型
    checkTypeName() {
      this.show = true;
    },
    // 打开日历
    openCalendar(idx) {
      this.idx = idx;
      this.showCalendar = true;
    },
    onConfirm(date) {
      const dateTime = formatDate(date);
      this.data[this.idx] = getDate(dateTime);
      this.showCalendar = false;
    },
    save() {
      console.log(this.data, "this.data");
      if (!this.data.ReportCode) {
        return this.$toast("请选择报告模板");
      }
      if (this.data.GenerateOption == 2) {
        if (!this.data.Name) {
          return this.$toast("请填写报告名称");
        }
      }

      if (!this.data.StartDate) {
        return this.$toast("请选择开始日期");
      }
      if (!this.data.EndDate) {
        return this.$toast("请选择结束日期");
      }
      this.loading = true;
      let form = {
        Codes: this.data.Codes,
        ReportCode: this.data.ReportCode,
        ReportType: this.data.ReportType,
        GenerateOption: this.data.GenerateOption,
        Type: this.data.Type,
        StartDate: this.data.StartDate,
        EndDate: this.data.EndDate,
        Remarks: this.data.Remarks,
        Name: this.data.Name,
      };
      let _this = this;
      _this.$axios
      
        .post("/OtherDangerReportJG/GenerateReport",form)
        .then((res)=>{
          _this.$toast("生成成功");
          _this.loading = false;
          setTimeout(() => {
            _this.title = "报告详情";
            _this.$router.push({
              name: "OtherCheckReportDetail",
              query: {
                url: _this.baseUrl + res.data.Attachs,
                id: res.data.ID,
                signState: res.data.SignState,
              },
            });
          }, 300);
        })
        .catch((err)=>{
          console.log("err", err);
        })
    },
  },
};
</script>

<style lang="less" scoped>
.check-task {
  background-color: #e8f1f8;
  padding-bottom: 20px;
}
.problem {
  margin-bottom: 10px;
}
.bottom-btns {
  margin-top: 10px;
  padding: 0 20px;

  .submit-btn {
    width: 100%;
    height: 30px;
    font-size: 12px;
  }
}
</style>
